package web.servlet;

import domain.Account;
import domain.Product;
import service.AccountService;
import service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class EditAccountServlet extends HttpServlet {
    private static final String EDIT_ACCOUNT = "/WEB-INF/jsp/account/editAccount.jsp";
    AccountService accountService = new AccountService();
    CatalogService catalogService = new CatalogService();
    Account oldAccount = new Account();
    private String msg;
    private String username;
    private String password;
    private String rpassword;
    private String email;
    private String firstName;
    private String lastName;
    private String status;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zip;
    private String country;
    private String phone;
    private String favouriteCategoryId;
    private String languagePreference;
    private boolean listOption;
    private boolean bannerOption;
    private String bannerName;
    private List<Product> myList;
    private boolean authenticated;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession httpsession = req.getSession();
        oldAccount =(Account) httpsession.getAttribute("loginAccount");
        this.username = oldAccount.getUsername();
        this.password = req.getParameter("password");
        this.rpassword = req.getParameter("rpassword");
        this.firstName = req.getParameter("firstName");
        this.lastName = req.getParameter("lastName");
        this.email = req.getParameter("email");
        this.phone = req.getParameter("phone");
        this.address1 = req.getParameter("address1");
        this.address2 = req.getParameter("address2");
        this.city = req.getParameter("city");
        this.state = req.getParameter("state");
        this.zip = req.getParameter("zip");
        this.country = req.getParameter("country");
        this.languagePreference = req.getParameter("languagePreference");
        this.favouriteCategoryId = req.getParameter("favouriteCategoryId");
        if(req.getParameter("listOption").equals("1")){
            this.listOption=true;
        }else{
            this.listOption=false;
        }
        if(req.getParameter("bannerOption").equals("1")){
            this.bannerOption=true;
        }else{
            this.bannerOption=false;
        }
        if (!validate()) {
            req.setAttribute("editMsg", this.msg);
            req.getRequestDispatcher(EDIT_ACCOUNT).forward(req, resp);
        }else {
            Account newAccount = accountService.getAccountByName(this.username);
            if(newAccount != null){
                newAccount.setPassword(this.password);
                newAccount.setFirstName(this.firstName);
                newAccount.setLastName(this.lastName);
                newAccount.setEmail(this.email);
                newAccount.setPhone(this.phone);
                newAccount.setAddress1(this.address1);
                newAccount.setAddress2(this.address2);
                newAccount.setCity(this.city);
                newAccount.setState(this.state);
                newAccount.setStatus("OK");
                newAccount.setZip(this.zip);
                newAccount.setCountry(this.country);
                newAccount.setLanguagePreference(this.languagePreference);
                newAccount.setFavouriteCategoryId(this.favouriteCategoryId);
                newAccount.setListOption(this.listOption);
                newAccount.setBannerOption(this.bannerOption);

            }
            accountService.updateAccount(newAccount);
            newAccount = accountService.getAccountByName(newAccount.getUsername());
            HttpSession session = req.getSession();
            session.setAttribute("loginAccount", newAccount);
            myList = catalogService.getProductListByCategory(newAccount.getFavouriteCategoryId());

            session.setAttribute("newAccount", newAccount);
            if (newAccount.isListOption()) {
                CatalogService catalogService = new CatalogService();
                List<Product> myList = catalogService.getProductListByCategory(newAccount.getFavouriteCategoryId());
                session.setAttribute("myList", myList);
            }
            resp.sendRedirect("editAccountForm");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(EDIT_ACCOUNT).forward(req,resp);
    }

    private boolean validate() {

        if(this.firstName == null || this.firstName.equals("")){
            this.msg = "firstName不能为空";
            return false;
        }
        if(this.lastName == null || this.lastName.equals("")){
            this.msg = "lastName不能为空";
            return false;
        }
        if (this.email != null && !this.email.equals("")){
            if (!this.email.matches("[\\w]+@[\\w]+.[\\w]+[\\w]")) {
                msg = "请输入有效的邮箱地址";
                return false;
            }
        } else {
            msg = "邮箱不能为空";
            return false;
        }
        if (this.phone == null || this.phone.equals("")){
            msg = "电话不能为空";
            return false;
        }
        if(this.address1 == null || this.address1.equals("")){
            this.msg = "地址一不能为空";
            return false;
        }
        if(this.address2 == null || this.address2.equals("")){
            this.msg = "地址二不能为空";
            return false;
        }
        if(this.city == null || this.city.equals("")){
            this.msg = "city不能为空";
            return false;
        }
        if(this.state == null || this.state.equals("")){
            this.msg = "state不能为空";
            return false;
        }
        if(this.zip == null || this.zip.equals("")){
            this.msg = "zip不能为空";
            return false;
        }
        if(this.country == null || this.country.equals("")){
            this.msg = "Country不能为空";
            return false;
        }
        return true;
    }
}
